/*
 * UserManageController.java
 * Copyright 2018 Qunhe Tech, all rights reserved.
 * Qunhe PROPRIETARY/CONFIDENTIAL, any form of usage is subject to approval.
 */

package com.bearox.trans.controller;

import com.bearox.trans.data.dto.UserDto;
import com.bearox.trans.data.vo.UserVo;
import com.bearox.trans.service.UserService;
import com.bearox.trans.utils.Result;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import static com.bearox.trans.controller.apis.UserManageApis.LOGIN;
import static com.bearox.trans.controller.apis.UserManageApis.REGISTER;

/**
 * @author Bearox
 */
@RestController
public class UserManageController {
    private final UserService userService;

    public UserManageController(final UserService userService) {
        this.userService = userService;
    }

    /**
     * 注册接口
     * @param userVo
     * @return
     */
    @PostMapping(REGISTER)
    public Result register(@RequestBody final UserVo userVo) {
        if (null == userVo || null == userVo.getPassword()) {
            return Result.error("invalid request");
        }
        final UserDto userDto = new UserDto();
        BeanUtils.copyProperties(userVo, userDto);
        final Long userId = userService.register(userDto, userVo.getPassword());
        return userId == null ? Result.error("failed register") : Result.ok(userId);
    }

    @PostMapping(LOGIN)
    public Result login(final HttpServletRequest request, final HttpServletResponse response,
            final String userName, final String password) {
        return Result.ok();
    }
}
